package com.yazi.mxz.person.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.myframework.smhj.hibernate.SearchingFactory;
import com.myframework.smhj.hibernate.Sorting;
import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.GoodsClassify;
import com.yazi.mxz.common.service.GoodsClassifyService;
import com.yazi.mxz.person.jackson.filter.GoodsClassifyFilter;

@Controller("goodsClassifyController")
@RequestMapping("/goodsClassify")
public class GoodsClassifyController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsClassifyController.class);

	@Resource
	GoodsClassifyService goodsClassifyService;

	@RequestMapping("/listWithSons/json")
	@ResponseBody
	public ListResult<?> listWithSonsJson(GoodsClassify classify) {
		log.debug("获取企业商品类目(包含子一级)");
		
		ListResult<GoodsClassify> jsonResult = new ListResult<GoodsClassify>();
		jsonResult.setFilter(GoodsClassifyFilter.getWithGoodsClassifies());
		
		// 按排序字段排序
		Sorting sorting = SearchingFactory.createSorting("sort", true);
		
		try {
			
			List<GoodsClassify> list = goodsClassifyService.list(classify, sorting);
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取企业商品类目异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		
		return jsonResult;
	}

	@RequestMapping("/listRoots/json")
	@ResponseBody
	public ObjectResult<?> listRootsJson(long companyId) {
		log.debug("获取根企业商品类目列表:companyId={}", companyId);
		ObjectResult<CustomGoodsClassify> jsonResult = new ObjectResult<CustomGoodsClassify>();
		jsonResult.setFilter(GoodsClassifyFilter.getWithGoodsClassifies());
		
		Company company = new Company();
		company.setCompanyId(companyId);
		
		GoodsClassify classfiy4s = new GoodsClassify();// GoodsClassify for search
		classfiy4s.setDeep(0);
		classfiy4s.setCompany(company);
		
		Sorting sorting = SearchingFactory.createSorting("sort", true);
		
		List<GoodsClassify> list;
		CustomGoodsClassify cgc;
		try {
			
			list = goodsClassifyService.list(classfiy4s, sorting);
			// 最终保持与listSons,listBrothers方法返回的结构一致,并解决排序问题,
			// 所以取代Set<GoodsClassify>.addAll(list)方式,
			cgc = new CustomGoodsClassify();
			cgc.setGoodsClassifies(list);
			
			jsonResult.setResult(cgc);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取根企业商品类目列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listSons/json")
	@ResponseBody
	public ObjectResult<?> listSonsJson(long classifyId) {
		log.debug("获取企业商品子类目列表:classifyId={}", classifyId);
		ObjectResult<GoodsClassify> jsonResult = new ObjectResult<GoodsClassify>();
		jsonResult.setFilter(GoodsClassifyFilter.getWithGoodsClassifies());
		
		GoodsClassify parent;
		try {
			parent = goodsClassifyService.one(classifyId);
			jsonResult.setResult(parent);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取企业商品子分类列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listBrothers/json")
	@ResponseBody
	public ObjectResult<?> listBrothersJson(long classifyId) {
		log.debug("获取企业商品兄弟类目列表:classifyId={}", classifyId);
		ObjectResult<GoodsClassify> jsonResult = new ObjectResult<GoodsClassify>();
		jsonResult.setFilter(GoodsClassifyFilter.getWithGoodsClassifies());
		try {
			GoodsClassify classify = goodsClassifyService.one(classifyId);
			if (null != classify.getGoodsClassify()) {
				jsonResult.setResult(classify.getGoodsClassify());
				jsonResult.setOk();
			} else {
				return listRootsJson(classify.getCompany().getCompanyId());
			}
		} catch (Exception e) {
			log.error("获取企业商品兄弟类目列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	public class CustomGoodsClassify {
		
		@JsonSerialize
		List<GoodsClassify> goodsClassifies;

		public List<GoodsClassify> getGoodsClassifies() {
			return goodsClassifies;
		}

		public void setGoodsClassifies(List<GoodsClassify> goodsClassifies) {
			this.goodsClassifies = goodsClassifies;
		}
	}
}
